package yxy.game.pm2.active.command.belovedhero;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actfavoritewarlordreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心仪武将活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouriteTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

/****************************************
 * @name CMD_Active_心仪武将_ActBelovedHeroLoginInfo
 * @description 心仪武将登录福利页面信息
 * @author 梁健彬
 * @since 2022-07-8
 ***************************************/
@MessageType(proto = Command.ActBelovedHeroLoginInfo, classtype = ActiveMessage.ActBelovedHeroLoginInfoRequest.class)
public class CMD_Active_心仪武将_ActBelovedHeroLoginInfo extends Command<ActiveMessage.ActBelovedHeroLoginInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        ActiveMessage.ActBelovedHeroLoginInfoResponse.Builder response = ActiveMessage.ActBelovedHeroLoginInfoResponse.newBuilder();

        try {
            ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(playercode, false);

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            ActBelovedHeroHandle heroHandle = new ActBelovedHeroHandle();
            ActiveMessage.ActBelovedHeroShop shop = heroHandle.getTaskShop(am);
            response.setShop(shop);
            //每日签到的礼包信息
            getTaskMsg(am, playercode, shop, response);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ActiveMessage.ActBelovedHeroLoginInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    /**
     * 付费和免费的任务信息
     */
    private void getTaskMsg(ActiveModule_心仪武将活动 am, String playercode, ActiveMessage.ActBelovedHeroShop shop, ActiveMessage.ActBelovedHeroLoginInfoResponse.Builder response) {
        int day = am.getDay();//天数
        int buynum = shop.getBuynum();//礼包购买次数
        ArrayList<ActiveMessage.ActBelovedHeroAllTask> list = new ArrayList<>();

        for (FavouriteTask ft : am.getTaskMap().values()) {
            ActiveMessage.ActBelovedHeroAllTask.Builder build = ActiveMessage.ActBelovedHeroAllTask.newBuilder();

            actfavoritewarlordreward data = DataCenter.getData(ft.getId(), actfavoritewarlordreward.class);
            build.setId(ft.getId());//id
            build.setFreeTaskId(ft.getFreeTaskId());//免费的奖励
            build.setPreTaskId(ft.getPreTaskId());//付费的奖励

            Task freeTaskBean = Task.get(playercode, ft.getFreeTaskCode());
            Task preTaskBean = Task.get(playercode, ft.getPreTaskCode());

            Currencies freeCurs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
            Currencies preCurs = new Currencies(Currency.create(data.getPrewardtype(), data.getPrewardid(), data.getPrewardnum()));
            Collection<PackageMessage.Item> freeCursItem = new CurrencyHandle().packItems(freeCurs);
            Collection<PackageMessage.Item> preCursItem = new CurrencyHandle().packItems(preCurs);
            build.addAllFreeTaskCurs(freeCursItem);//奖励
            build.addAllPreTaskCurs(preCursItem);//奖励

            int freeTaskCanget = freeTaskBean.isReward() ? DrawStatus.hadget.getStatus() : freeTaskBean.isFinish() && day >= ft.getSort()? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
            int preTaskCanget = preTaskBean.isReward() ? DrawStatus.hadget.getStatus() : preTaskBean.isFinish() && day >= ft.getSort() && buynum > 0 ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
            build.setFreeCursCanget(freeTaskCanget);//是否可领取
            build.setPreCursCanget(preTaskCanget);//是否可领取
            list.add(build.build());
        }

        //从低到高排序
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                ActiveMessage.ActBelovedHeroAllTask builder1 = (ActiveMessage.ActBelovedHeroAllTask) o1;
                ActiveMessage.ActBelovedHeroAllTask builder2 = (ActiveMessage.ActBelovedHeroAllTask) o2;
                return builder1.getId() - builder2.getId();
            }
        });

        response.addAllTaskMsg(list);

    }


}
